<?
// init.php
// written by:    David Fudge [ rkstar@mac.com ]
// created on:    November 1, 2008
// last modified: November 1, 2008
//
// description:
// this file contains the main class that is used by the "ed" framework.

// "ed" is a static class only used to require data...
class ed
{
	// load a package api
	public static function load( $pkg )
	{
		// sanity
		if( strlen($pkg) < 1 ) { return false; }

		// we'll replace the "."s with "/"s
		$path = (strstr($pkg,".")) ? str_replace(".","/",$pkg) : $pkg;
		// find the last dir in the path and the package requested
		$lastdir = (strstr($path,"/")) ? substr($path,0,strrpos($path,"/")) : "";
		$package = (strstr($path,"/")) ? substr($path,strrpos($path,"/") + 1) : $path;
		$pkgdir  = (strlen($lastdir) > 0) ? LIB."/".$lastdir : LIB;

		if( !is_dir($pkgdir) ) { return false; }

		// see if we've already included this file?
		if( in_array(LIB."/".$path, get_included_files()) ) { return false; }
	    
		// we have a dir... include the package(s)
		if( $package == "*" )
		{
			// we want ALL the packages within this directory
			// read the dir and loop it
			if( !($dh = opendir($pkgdir)) ) { return false; }
			while( $file = readdir($dh) )
			{
				if( ($file==".") || ($file=="..") )  { continue; }
				elseif( substr($file,-4) == ".php" ) { require_once($pkgdir."/".$file); }
			}
		}
		else { if( file_exists(LIB."/".$path.".php") ) { require_once(LIB."/".$path.".php"); } }
	}
	
}
// NOTE: about form submission...
// we can accept both regular form submission through GET or POST but we can also
// accept XML form submission through the php://input data.
// if XML data is posted to our form, it will take precedence over regular form
// submission data.  HOWEVER, we will load form data first, then the XML data,
// so if there is a combination of data submitted and the variable names are all
// unique, ALL DATA will be available in our application.
//
// easily get form values
function _form( $var )
{
	global $_GET, $_POST, $_REQUEST;
	// check for magic_quotes
	$mq_on = (get_magic_quotes_gpc() != 0) ? true : false;
	// in this order...
	// each will override the previous!
	if( array_key_exists($var,$_REQUEST) ) { return (!$mq_on && is_string($_REQUEST[$var])) ? addslashes($_REQUEST[$var]) : $_REQUEST[$var]; }
	if( array_key_exists($var,$_POST) ) { return (!$mq_on && is_string($_POST[$var])) ? addslashes($_POST[$var]) : $_POST[$var]; }
	if( array_key_exists($var,$_GET) ) { return (!$mq_on && is_string($_GET[$var])) ? addslashes($_GET[$var]) : $_GET[$var]; }
	
	return null;
}
// create a variable that will NOT interfere with any normal
// variable namespace...
$__php_input_vars_loaded_by_ED_core_library = file_get_contents("php://input");
if( strlen($__php_input_vars_loaded_by_ED_core_library) > 0 )
{ $__simple_xml_php_input_vars_loaded_by_ED_core_library = @simplexml_load_string($__php_input_vars_loaded_by_ED_core_library); }
// a simple function to get xml loaded variables from the php://input
function _xml( $var )
{
	global $__simple_xml_php_input_vars_loaded_by_ED_core_library;
	// check for it and return accordingly
	return (isset($__simple_xml_php_input_vars_loaded_by_ED_core_library->$var)) ? $__simple_xml_php_input_vars_loaded_by_ED_core_library->$var : null;
}
?>